package es.diezfm.mobile;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;

public class BackgroundAudioService extends Service implements OnCompletionListener {

	static MediaPlayer mMediaPlayer;

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onCreate() {
	  super.onCreate();
	  mMediaPlayer = new MediaPlayer();
		
		try {
			mMediaPlayer.setDataSource("http://stream.giss.tv:8000/D1EZFM.mp3");
			mMediaPlayer.prepare();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    
    return START_STICKY;
  }

  public void onDestroy() {
    if (mMediaPlayer.isPlaying()) {
      mMediaPlayer.stop();
    }
    mMediaPlayer.release();
  }

  public void onCompletion(MediaPlayer _mediaPlayer) {
    stopSelf();
  }

}